const LogModel = require('../../model/Log');
const moment = require('moment')
var async = require('async')
const request = require('request')
const schedule = require('node-schedule');
const User = require('../../model/User')
const NewPrivateuser = require('../../model/Newprivateuser')
const Privateuser = require('../../model/Privateuser');
const AdminUser = require('../../model/AdminUser');
const Chatlistuser = require('../../model/Chatlistuser')
const ShowChatList = require('../../model/ShowChatList')
// const TencentCloudSMS = require('../../util/businessUtil/TencentCloudSMS3.0.js')

module.exports = {
    getIndexDetail: async(req, res, next) =>{
        try {
            let options = {}
            // options.isDelete = 0
            if(req.body.uid){
                options.uid = new RegExp(req.body.uid, 'i')
            }
            if(req.body.nickname){
                options.nickname = new RegExp(req.body.nickname, 'i')
            }
            if(req.body.date){
                let startDate = new Date(req.body.date[0])
                let endDate = new Date(req.body.date[1])
                options.addTime = {$gte:startDate,$lte:endDate}
            }
            let count = await NewPrivateuser.countDocuments(options).exec()
            let notClickedCount =  await NewPrivateuser.countDocuments(Object.assign({}, options,{isClicked:{$ne:1}})).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let newPrivateuserList = await NewPrivateuser.find(options).sort({updatetime:-1}).skip((page - 1) * pageSize).limit(pageSize).populate({
                path: 'firstClickUser.staff',
                model: AdminUser
            }).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.pageSize = pageSize
            result.notClickedCount = notClickedCount
            result.data = newPrivateuserList
            res.ok('当日数据查询成功',result)
        } catch (e) {
            next(e)
        }
    },
    
    getNewChatDetail: async(req, res, next) =>{
        try {
            let showChatList = await ShowChatList.findOne().exec()
            let options = {}
            options.isDelete = {$ne:1}
            if(req.body.uid){
                options.uid = new RegExp(req.body.uid, 'i')
            }
            if(req.body.nickname){
                options.nickname = new RegExp(req.body.nickname, 'i')
            }
            if(req.body.date){
                let startDate = new Date(req.body.date[0])
                let endDate = new Date(req.body.date[1])
                options.addTime = {$gte:startDate,$lte:endDate}
            }
            let count = await Chatlistuser.countDocuments(options).exec()
            let notClickedCount =  await Chatlistuser.countDocuments(Object.assign({}, options,{isClicked:{$ne:1}})).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let tempnewPrivateuserList =[]
            let start40 = true
            if(req.body.uid){
                start40 = false
            }
            if(req.body.nickname){
                start40 = false
            }
            if(req.body.date){
                start40 = false
            }
            if(!start40){
                tempnewPrivateuserList = await Chatlistuser.find(options).sort({_id:-1}).skip((page - 1) * pageSize).limit(pageSize).populate({
                    path: 'firstClickUser.staff',
                    model: AdminUser
                }).exec()
            }else{
                if((page*pageSize)<40){
                    let start = (page-1)*pageSize
                    let end = page*pageSize
                    let tempuidlist = []
                    showChatList.uidlist.map(e=>{if(e!=null){tempuidlist.push(e)}})
                    let searchuidlist = tempuidlist.slice(start,end)
                    options.uid = {$in:searchuidlist}
                    tempnewPrivateuserList = await Chatlistuser.find(options).sort({_id:-1}).populate({
                        path: 'firstClickUser.staff',
                        model: AdminUser
                    }).exec()
                }else{
                    options.uid = {$nin:showChatList.uidlist}
                    tempnewPrivateuserList = await Chatlistuser.find(options).sort({_id:-1}).skip((page - 1) * pageSize).limit(pageSize).populate({
                        path: 'firstClickUser.staff',
                        model: AdminUser
                    }).exec()
                }
            }
            let result = {}
            result.total = count
            result.currentPage = page
            result.pageSize = pageSize
            result.notClickedCount = notClickedCount
            result.data = tempnewPrivateuserList
            res.ok('查询成功',result)
        } catch (e) {
            next(e)
        }
    }
};

const dailyCronstyle = () => {
	schedule.scheduleJob('0 0 0 * * *', () => {
        console.log('每日统计:' + new Date());
	});
}

dailyCronstyle();